import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_shop/cons/global_cons.dart';
import 'package:flutter_shop/pages/cart/cart_router.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/entity/cart_goods_data.dart';
import 'package:flutter_shop/pages/cart/cart_tab_page/provider/cart_provider.dart';
import 'package:flutter_shop/pay/alipay/ali_pay_result_data.dart';
import 'package:flutter_shop/pay/alipay/alipay.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/water_click.dart';
import 'package:provider/provider.dart';
import 'package:tobias/tobias.dart';

import 'package:pay_local_sign/pay_local_sign.dart';

///购物车底部结算
class CartBottomBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60,
      padding: Paddings.left_right,
      decoration: BoxDecoration(
        color: CommonColors.white,
        border: Borders.bottom,
      ),
      child: Row(
        children: <Widget>[
          _checkBox(context),
          Gaps.h_gap_10,
          Expanded(child: _centerPrice(context)),
          Gaps.h_gap_10,
          _submitCart(context),
        ],
      ),
    );
  }

  Widget _checkBox(BuildContext context) {
    bool _checked = Provider.of<CartProvider>(context).isCheckAll;

    return WaterClick(
      child: Row(
        children: <Widget>[
          Icon(
            _checked ? Icons.check_circle : Icons.radio_button_unchecked,
            color: _checked ? CommonColors.red : CommonColors.gray,
            size: 25.0,
          ),
          Gaps.h_gap_10,
          Text("全选"),
        ],
      ),
      onPressed: () {
        Provider.of<CartProvider>(context, listen: false).checkAll(!_checked);
      },
    );
  }

  Widget _centerPrice(BuildContext context) {
    double totalPrice = Provider.of<CartProvider>(context).totalPrice;

    return Container(
      alignment: Alignment.centerRight,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: <Widget>[
              Text(
                "合计: ",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: CommonDimens.font_size_xmiddle),
              ),
              Gaps.h_gap_10,
              Text(
                "¥ $totalPrice",
                style: TextStyle(
                  fontSize: CommonDimens.font_size_xmiddle,
                  color: CommonColors.red,
                ),
              ),
            ],
          ),
          Text(
            "满99元免配送费,预购免配送费",
            style: TextStyle(
              fontSize: CommonDimens.font_size_xsmall,
              color: CommonColors.gray,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }

  Widget _submitCart(BuildContext context) {
    int totalCount = Provider.of<CartProvider>(context, listen: false).totalCount;
    List<CartGoodsData> goodsList = Provider.of<CartProvider>(context).cartGoodsList;

    return RaisedButton(
      padding: EdgeInsets.symmetric(vertical: 12),
      color: CommonColors.red,
      onPressed: () {
        if (totalCount < 1) {
          Utils.showToast("请随便买几样吧!");
          return;
        }

        List<CartGoodsData> list = goodsList.where((v) => v.checked).toList();

        NavigatorUtils.pushCheckLogin(context, CartRouter.orderConfirm, params: {GlobalCons.KEY_DATA: Utils.parseObj2Json(list)});
      },
      child: Text(
        "结算 ($totalCount)",
        style: TextStyle(color: Colors.white),
      ),
    );
  }
}
